package com.zzk.lanouhn.myapplication1.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.NewestLikeAdapter;
import com.zzk.lanouhn.myapplication1.modles.CollectSingleGood;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 最近喜欢页面
 * Created by lanouhn on 16/9/18.
 */
public class NewestLikeActivity extends AppCompatActivity{

    private ImageView iv_back;
    private RecyclerView recyclerView;
    private String singleGoodId;
    private List<CollectSingleGood.DataBean.LikesListBean> likelist = new ArrayList<>();
    private NewestLikeAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newestlike);
        iv_back= (ImageView) findViewById(R.id.iv_back_newestlike);
        recyclerView= (RecyclerView) findViewById(R.id.recy_like_user_newest);
        initData();
        initEvent();
        getData();
    }
    private void initData(){
        SharedPreferences preferences=this.getSharedPreferences("user", Context.MODE_PRIVATE);
        singleGoodId=preferences.getString("singleGoodId","default");
    }
    private void initEvent(){
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void getData(){
        String url1 = "http://open4.bantangapp.com/product/newInfo?";
        String dataUrl1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2" +
                "&screensize=720" +
                "&v=19&id="+singleGoodId+"&trace_id=&page=0" +
                "&pagesize=10&comments_pagesize=10";
        try {
            HttpUtils.doPostAsyn(url1, dataUrl1, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson1 = new Gson();
                    likelist = gson1.fromJson(result, CollectSingleGood.class).getData().getLikes_list();
                    if (likelist.size() > 0) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   private Handler handler=new Handler(){
       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
           switch(msg.what){
               case 0:

                   break;
               case 1:
                   adapter=new NewestLikeAdapter(NewestLikeActivity.this,likelist);
                   recyclerView.setAdapter(adapter);
                   LinearLayoutManager manager=new LinearLayoutManager(NewestLikeActivity.this);
                   manager.setOrientation(LinearLayoutManager.VERTICAL);
                   recyclerView.setLayoutManager(manager);
                   break;
             default:

                 break;
           }
       }
   };

}
